<template>
    <div>
        <div class="el-header">
            <div class="header-main">
                <el-row>
                    <el-col :span="4">
                        <h1 class="name">简途论坛</h1>
                    </el-col>
                    <el-col :span="2"> 
                        <router-link tag="div" to="/" class="go-home">
                           首页
                        </router-link>
                    </el-col>
                    <el-col :span="12">
                          <div class="header-seacher">
                                <el-input v-model="seacherState" placeholder="请输入内容" class="seacher-input" >
                                    <i class="el-icon-search el-input__icon" slot="suffix" @click="handleIconClick"></i>
                                </el-input>
                            </div>
                    </el-col>
                    <el-col :span="6">
                        <div class="header-login">
                            <el-button type="primary" class="questions"  @click="isQustion">提问</el-button>
                          <i class="el-icon-message el-input__icon" slot="suffix"></i>
                          <div class="login-success logins" v-if="isLogin">
                                <el-dropdown split-button type="primary" @command="cancellation">
                                       {{username}}
                                        <el-dropdown-menu slot="dropdown">
                                            <el-dropdown-item command="a">
                                                我的问题
                                                </el-dropdown-item>
                                            <!-- <el-dropdown-item command="b">
                                                设置
                                                </el-dropdown-item> -->
                                            <el-dropdown-item command="c">
                                                退出
                                              </el-dropdown-item>
                                              <el-dropdown-item command="d">
                                                 用户列表
                                                </el-dropdown-item>
                                        </el-dropdown-menu>
                                </el-dropdown>
                            </div>
                            <div class="login-no logins" v-if="!isLogin" @click="goLogin">
                                登录
                            </div>
                        </div>
                    </el-col>
                </el-row>
            </div>
        </div>
        <!-- 模态框 -->
        <el-dialog title="写下你的问题" :visible.sync="centerDialogVisible" center width="30%">
            <el-form  :model="form" :rules="rules" ref="form">
                <div class="question-title">问题题目</div>
                <div class="form-item">
                    <el-form-item prop="name">
                        <el-input v-model="form.name" auto-complete="off" placeholder="问题题目"></el-input>
                   </el-form-item>
                </div>
                <div class="question-title">问题内容</div>
                <div class="form-item">
                     <el-form-item prop="textarea">
                        <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.textarea"></el-input>
                     </el-form-item>
                </div>
                
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="FormVisibleData('form')">提 交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "vHeader",
  data() {
    return {
      seacherState: "",
      centerDialogVisible: false,
      username: "",
      isLogin: "",
      form: {
        name: "",
        textarea: ""
      },
      rules: {
        name: [{ required: true, message: "请输入你的问题", trigger: "blur" }],
        textarea: [
          { required: true, message: "请输入你的答案", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handleIconClick() {
      this.axios.get('/homeApl/question/search?keyWord='+this.seacherState)
            .then( (response) => {
              if(response.data.code == 0){
                  this.$emit('seacherData',response.data.result);
              }
            })
            .catch( (error) => {
              console.log(error);
            });
    },
    isQustion() {
      if (this.isLogin) {
        this.centerDialogVisible = true;
      } else {
        this.$confirm("是否现在去登录?", "未登录", {
          confirmButtonText: "登录 / 注册",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.$router.push("login");
          })
          .catch(() => {});
      }
    },
    goLogin() {
      this.$router.push("login");
    },
    FormVisibleData(form) {
      let queData = {
        userId:localStorage.userId,
        title:this.form.name,
        content:this.form.textarea
      }
      this.$refs[form].validate(valid => {
        if (valid) {
         this.axios.post('/homeApl/question', queData)
            .then( (response) => {
              if(response.data.code == 0){
                  this.centerDialogVisible = false;
                  this.form.name = '';
                  this.form.textarea = '';
                  this.$emit('refurbished');
              }
            })
            .catch( (error) => {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getUsername() {
      if (localStorage.username) {
        this.username = localStorage.username;
        this.isLogin = true;
      } else {
        this.isLogin = false;
      }
    },
    cancellation(command) {
      if (command == "c") {
        localStorage.clear();
        this.isLogin = false;
        this.changeName("");
        this.changeRole("");
        this.$router.push({ path: '/' });
      }
      if(command == "d"){
        this.$router.push({ path: '/usersList' });
      }
      if(command == "a"){
        this.axios.get('/homeApl/question/search?keyWord='+localStorage.userId+'&searchType=userId')
            .then( (response) => {
              if(response.data.code == 0){
                  this.$emit('seacherData',response.data.result);
              }
            })
            .catch( (error) => {
              console.log(error);
            });
      }
    },
    ...mapMutations(["changeName","changeRole"])
  },
  mounted() {
    this.getUsername();
  }
};
</script>

<style scoped lang='scss'>
.el-header {
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  .header-main {
    box-sizing: border-box;
    width: 1000px;
    margin: 0px auto;
    background: #d5dee5;
    height: inherit;
    line-height: 60px;
    display: flex;
    flex-direction: row;
    padding: 0 20px;
    .el-row {
      width: inherit;
    }
    h1 {
      margin: 0px;
      text-indent: 20px;
      color: #cc5252;
      font-family: "Xingkai SC";
      //   text-align: center;
    }
    .go-home {
      text-align: center;
      cursor: pointer;
    }
    .header-seacher {
      text-align: center;
    }
    .header-login {
      text-align: center;
      .questions {
        margin-right: 20px;
      }
      cursor: pointer;
      .logins {
        display: inline-block;
      }
      .login-no {
        color: #3a8ee6;
      }
    }
  }
}
.question-title {
  text-indent: 10px;
  font-weight: bolder;
  font-size: 16px;
}
.form-item {
  margin: 10px 10px;
}
</style>